<template>
    <div class="mainDiv" style="padding: 10px;box-sizing: border-box;">
        <div class="rowHead">
            <el-input style="width:300px" v-model="page.search" placeholder="请输入厂商名称或描述" @keyup.enter.native="searchList">
                <i slot="suffix" class="el-icon-search" style="line-height: 40px;width: 25px;cursor: pointer;" @click="searchList"></i>
            </el-input>
        </div>
        <el-table :data="list" :stripe="true" @row-click="danji" @row-dblclick="dblclick" height="100%">
            <el-table-column prop="id" label="厂商ID"></el-table-column>
            <el-table-column prop="name" label="厂商名称"></el-table-column>
            <el-table-column prop="remarks" label="厂商描述" :show-overflow-tooltip="true"></el-table-column>
            <el-table-column prop="addtime" label="添加时间">
                <template scope="scope">
                    {{time(scope.row.addtime)}}
                </template>
            </el-table-column>
            <el-table-column label="选择">
                <template scope="scope">
                    <el-switch v-model="scope.row.selected" on-text="是" off-text="否">
                    </el-switch>
                </template>
            </el-table-column>
        </el-table>
        <div class="block center bg-light">
            <el-col :span="12">
                <el-pagination @current-change="currentChange" :current-page="page.pages" :page-size="page.limits" layout="total, prev, pager, next" :total="total">
                </el-pagination>
            </el-col>
            <el-col :span="12" style="text-align: right;">
                <el-button size="mini" icon="close" type="danger" @click="close">取消关闭</el-button>
                <el-button size="mini" icon="upload2" type="success" @click="submit">确定选择</el-button>
            </el-col>
        </div>
        <div style="text-align: right">
        </div>
    </div>
</template>
<script>
import C from '#/src/common/common.js'
export default {
    name: 'selectfactory',
    data() {
        return {
            list: [],
            total: 0,
            page: {
                pages: 1,
                limits: 15,
                search: '',
            },
            select: {}
        }
    },
    mounted() {
        this.updateList()
    },
    methods: {
        danji: function(row, event) {
            event.preventDefault()
            this.list.forEach((col) => {
                col.selected = false
            })
            row.selected = true
            this.select = row
        },
        dblclick: function(row, event) {
            event.preventDefault()
            C.closeBrowserWindow(row)
        },
        submit: function() {
            if (!this.select.id) {
                C.error('请选择厂商')
                return false
            }
            C.closeBrowserWindow(this.select)
        },
        time: function(unix) {
            let tm = new Date(unix * 1000)
            let yy = tm.getFullYear()
            let mm = tm.getMonth() < 9 ? '0' + (tm.getMonth() + 1) : tm.getMonth() + 1
            let dd = tm.getDate() < 10 ? '0' + tm.getDate() : tm.getDate()
            return yy + '-' + mm + '-' + dd
        },
        refresh: function() {
            window.location.reload()
        },
        close: function() {
            C.closewin()
        },
        searchList: function() {
            this.updateList()
        },
        currentChange: function(val) {
            this.page.pages = val
            this.updateList()
        },
        updateList: async function() {
            let data = await C.ajax('/organization/popup/factorylist', this.page, 'post')
            if (data) {
                this.total = data.count
                data.data.forEach((row) => {
                    row.selected = false
                })
                this.list = data.data
            }
        },
    }
}
</script>
<style>
.el-table tr:hover {
    cursor: pointer;
}
</style>